<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form ref="userForm" label-width="120px" :model="userInfo" :rules="rules" style="margin-left: 120px; margin-top:30px">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfo.username" style="width:300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" disabled style="width:300px" />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <upload-img :img-url="userInfo.staffPhoto" @get-url="geturl" />
          </el-form-item>
          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="confirm">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById, updataempioyeeAPI } from '@/api/user'
export default {
  data() {
    return {
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '名称为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getdetail()
  },
  methods: {
    async getdetail() {
      const res = await getUserDetailById(this.$route.query.id)
      this.userInfo = res
    },
    confirm() {
      this.$refs.userForm.validate(async valid => {
        if (valid) {
          await updataempioyeeAPI(this.userInfo)
          this.$router.back()
        }
      })
    },
    geturl(url) {
      this.userInfo.staffPhoto = url
    }
  }
}
</script>
